Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fixes #4297 Support array numbers in pathOfError for ErrorSchemaBuilder #4313

Conversation

kylie-marshall
Copy link
Contributor

Reasons for making this change

fixes #4297
Using ErrorSchemaBuilder to add errors to arrays results in the wrong format being generated. This PR extends the ErrorSchemaBuilder to support adding, setting and clearing errors that have an array number in their path.
Uses lodash https://lodash.com/docs/4.17.15#setWith to set the numbers as object instead of arrays.

const errorSchemaBuilder = new ErrorSchemaBuilder();
errorSchemaBuilder.addErrors('is required', ['array', 0, 'field']);
errorSchemaBuilder.ErrorSchema

Before change:

{
    "array": [
        {
            "field2": {
                "__errors": [
                    "is required"
                ]
            }
        }
    ]
}

After change:

{
    "array": {
        "0": {
            "field2": {
                "__errors": [
                    "is required"
                ]
            }
        }
    }
}

The following also work

errorSchemaBuilder.addErrors('is required', 'array.0.field');
errorSchemaBuilder.addErrors('is required', ['array', '0', 'field']);
errorSchemaBuilder.addErrors('is required', 'array["0"].field');

This is my first contribution to this repo so open to feedback. Due to timezone differences, I can't attend the meeting.

Checklist

  • I'm updating documentation
  • I'm adding or updating code
    • I've added and/or updated tests. I've run npx nx run-many --target=build --exclude=@rjsf/docs && npm run test:update to update snapshots, if needed.
    • I've updated docs if needed
    • I've updated the changelog with a description of the PR
      I'm not sure what to add to the change log
  • I'm adding a new feature
    • I've updated the playground with an example use of the feature

Copy link
Contributor

@nickgros nickgros left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thanks @kyliem29 ! We'll need to add this to the CHANGELOG but we can do that before we cut a release

@nickgros nickgros merged commit f7e05bb into rjsf-team:main Sep 27, 2024
5 checks passed
@nickgros nickgros mentioned this pull request Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ToErrorList doesn't return array errors
2 participants